<template>
  <div class="login-container">
    <a-card class="login-card" title="系统登录" :bordered="false">
      <!-- 添加标题样式 -->
      <div slot="title" class="login-title">
        <div class="title-icon"><LockOutlined /></div>
        <h2>账户登录</h2>
      </div>

      <a-form
        ref="formRef"
        :model="formState"
        :rules="rules"
        layout="vertical"
        class="login-form"
      >
        <!-- 用户名输入框 - 添加动画容器 -->
        <div class="form-item-container">
          <a-form-item name="username" label="用户名">
            <a-input
              v-model:value="formState.username"
              placeholder="请输入用户名"
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <user-outlined class="input-icon" />
              </template>
            </a-input>
          </a-form-item>
        </div>

        <!-- 密码输入框 - 添加动画容器 -->
        <div class="form-item-container">
          <a-form-item name="password" label="密码">
            <a-input-password
              v-model:value="formState.password"
              placeholder="请输入密码"
              size="large"
              visibilityToggle
              class="custom-input"
            >
              <template #prefix>
                <lock-outlined class="input-icon" />
              </template>
            </a-input-password>
          </a-form-item>
        </div>

        <!-- 操作链接区域 -->
        <div class="form-actions">
          <a-checkbox
            v-model:checked="formState.remember"
            class="remember-checkbox"
          >
            <span class="checkbox-text">记住我</span>
          </a-checkbox>
          <div class="form-links">
            <a @click="handleForgotPassword" class="link-item">忘记密码</a>
            <a @click="handleRegister" class="link-item">注册账号</a>
          </div>
        </div>

        <!-- 登录按钮 - 添加渐变和动画 -->
        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            class="login-button"
            size="large"
            block
            @click.prevent="handleLogin"
          >
            <span class="button-text">登录</span>
            <LoadingOutlined class="loading-icon" v-if="loading" />
          </a-button>
        </a-form-item>
      </a-form>

      <!-- 底部版权信息 -->
      <div class="login-footer">
        <p>© 2023 系统管理平台 - 版权所有</p>
      </div>
    </a-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import {
  UserOutlined,
  LockOutlined,
  LoadingOutlined,
} from "@ant-design/icons-vue";
import { message } from "ant-design-vue";
import { useRouter } from "vue-router";
import { handleLoginApi } from "../api/user";
import { useUserStore } from "../store/module/user";
const userStore = useUserStore();
const formRef = ref(null);
const router = useRouter();

const formState = reactive({
  username: "",
  password: "",
});

const rules = {
  username: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
    {
      min: 6,
      message: "密码长度不能少于6位",
      trigger: "blur",
    },
  ],
};

const loading = ref(false);

const handleLogin = async () => {
  try {
    // 表单验证
    await formRef.value.validate();

    // 模拟登录请求
    // setTimeout(() => {
    //   if (formState.username && formState.password) {
    //     message.success("登录成功");
    //     router.push("/home");
    //   } else {
    //     message.error("用户名或密码错误");
    //   }
    // }, 800);
    const res = await handleLoginApi(formState);
    // console.log(res);
    if (res.code == 200) {
      userStore.username = formState.username;
      userStore.token = res.data.token;
      userStore.userInfo = res.data.userInfo;
      // await userStore.getUserInfo()
      message.success("登录成功!");
      router.push("/home");
    }

  } catch (error) {
    // console.log(error);
    message.error(error.message);
  }
};
</script>

<style scoped>
/* 容器样式优化 */
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background-size: 400% 400%;
  animation: gradientBackground 15s ease infinite;
}

/* 卡片样式重构 */
.login-card {
  width: 100%;
  max-width: 420px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  border-radius: 20px;
  overflow: hidden;
  transition: all 0.3s ease;
  background-color: #fff;
}

.login-card:hover {
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
  transform: translateY(-5px);
}

/* 标题样式 */
.login-title {
  text-align: center;
  padding: 30px 0 15px;
}

.title-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 15px;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  box-shadow: 0 4px 15px rgba(24, 144, 255, 0.3);
}

.login-title h2 {
  margin: 0 0 8px;
  font-size: 22px;
  color: #1f2937;
  font-weight: 600;
}

.title-desc {
  color: #6b7280;
  font-size: 14px;
  margin: 0;
}

/* 表单样式优化 */
.login-form {
  padding: 15px 32px 25px;
}

/* 表单项容器 - 添加动画效果 */
.form-item-container {
  position: relative;
  margin-bottom: 18px;
}

/* 自定义输入框样式 */
.custom-input {
  border-radius: 12px;
  border-color: #e5e7eb;
  height: 52px;
  font-size: 15px;
  transition: all 0.3s ease;
}

.custom-input:hover {
  border-color: #94a3b8;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.05);
}

.custom-input:focus-within {
  border-color: #1890ff;
  box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.15);
  border-width: 2px;
}

/* 输入框图标样式 */
.input-icon {
  color: #94a3b8;
  font-size: 18px;
  transition: color 0.3s ease;
}

.custom-input:focus-within .input-icon {
  color: #1890ff;
}

/* 表单操作区域 */
.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 8px 0 25px;
}

/* 复选框样式 */
.remember-checkbox .ant-checkbox-inner {
  border-radius: 6px;
  width: 16px;
  height: 16px;
}

.checkbox-text {
  color: #4b5563;
  font-size: 14px;
  margin-left: 6px;
}

/* 链接样式 */
.form-links {
  display: flex;
  gap: 20px;
}

.link-item {
  color: #3b82f6;
  font-size: 14px;
  text-decoration: none;
  transition: all 0.2s ease;
  position: relative;
}

.link-item:hover {
  color: #2563eb;
}

.link-item::after {
  content: "";
  position: absolute;
  width: 0;
  height: 1px;
  bottom: -2px;
  left: 0;
  background-color: #3b82f6;
  transition: width 0.2s ease;
}

.link-item:hover::after {
  width: 100%;
}

/* 登录按钮样式 - 渐变效果 */
.login-button {
  height: 54px;
  font-size: 16px;
  border-radius: 12px;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  border: none;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-button:hover {
  background: linear-gradient(135deg, #096dd9 0%, #1890ff 100%);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(24, 144, 255, 0.25);
}

.login-button:active {
  transform: translateY(0);
}

/* 按钮文本和加载图标 */
.button-text {
  position: relative;
  z-index: 2;
}

.loading-icon {
  margin-left: 8px;
  animation: spin 1.5s linear infinite;
}

/* 底部版权信息 */
.login-footer {
  text-align: center;
  padding: 15px 0;
  border-top: 1px solid #f3f4f6;
  margin-top: 10px;
}

.login-footer p {
  margin: 0;
  color: #9ca3af;
  font-size: 13px;
}

/* 动画定义 */
@keyframes gradientBackground {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 响应式调整 */
@media (max-width: 480px) {
  .login-card {
    width: 100%;
    margin: 0 15px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  }

  .login-form {
    padding: 15px 24px 20px;
  }

  .form-actions {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .form-links {
    align-self: flex-end;
  }
}
</style>
